import 'dart:convert';
import 'WeatherWidget.dart';
import 'package:flutter/material.dart';
import 'package:FlutterApp/City.dart';
import 'package:http/http.dart' as http;

class CityWidget extends StatefulWidget {

  @override
  State<StatefulWidget> createState() {
    return CityState();
  }

}

class CityState extends State<CityWidget> {
  List<City> data = new List();

  CityState() {
    getData();
  }

  void getData() async {
    List<City> cityList = await getList();

    setState(() {
      data = cityList;
    });
  }

  // ignore: missing_return
  Future<List<City>> getList() async {
    List<City> list = new List();
    City city1 = City(name: "北京市", id: "101010100");
    City city2 = City(name: "上海市", id: "101020100");
    City city3 = City(name: "天津市", id: "101030100");
    City city4 = City(name: "重庆市", id: "101040100");
    list.add(city1);
    list.add(city2);
    list.add(city3);
    list.add(city4);
    return list;
  }

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemCount: data.length,
      itemBuilder: (context, index) {
        return ListTile(
          title: GestureDetector(
            child: Container(
              color: Colors.deepPurpleAccent,
              padding: EdgeInsets.all(10),
              alignment: Alignment.center,
              margin: EdgeInsets.only(bottom: 1),
              child: Text(data[index].name, style: TextStyle(
                  color: Colors.amber
              )
              ),
            ),
            onTap: () {
              Navigator.push(context,
                  MaterialPageRoute(builder: (context) => WeatherWidget(city: data[index])));
            },
          ),
        );
      });
  }

}